package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.VO.TbSpecificationVO;
import com.dongdongshop.service.TbSpecificationService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("spec")
public class TbSpecificationController {

    @DubboReference
    private TbSpecificationService tbSpecificationService;

    @RequestMapping("tospecification")
    public String tospecification() {
        return "/admin/specification";
    }

    @RequestMapping("specList")
    @ResponseBody
    public Result specList(TbSpecification tbSpecification) {
        try {
            List<TbSpecification> tbSpecifications = tbSpecificationService.selectBy(tbSpecification);
            return Result.response(ResultEnum.SUCCESS).setData(tbSpecifications);
        } catch (Exception e) {

            return Result.response(ResultEnum.ERROR).setData(e);
        }

    }

    @RequestMapping("add")
    @ResponseBody
    public Result add(String jsonList, TbSpecification tbSpecification) {
        List<TbSpecificationOption> options = JSONObject.parseArray(jsonList,TbSpecificationOption.class);
        boolean b = tbSpecificationService.insertBy(options, tbSpecification);
        if (b){
            return Result.response(ResultEnum.SUCCESS);
        }
        return Result.response(ResultEnum.ERROR);
    }

    @RequestMapping("deleteByIds")
    @ResponseBody
    public Result deleteByIds(@RequestParam("ids") Integer[] ids) {
            boolean b = tbSpecificationService.deleteById(ids);
            if (b){
                return Result.response(ResultEnum.SUCCESS);
            }
            return Result.response(ResultEnum.ERROR);
    }

    @RequestMapping("toupda")
    @ResponseBody
    public Result toupda(Long id){
        try{
            List<TbSpecificationVO> voList = tbSpecificationService.selectByVO(id);
            return Result.response(ResultEnum.SUCCESS).setData(voList);
        }catch (Exception e){
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }
    @RequestMapping("update")
    @ResponseBody
    public Result update(String jsonlist, TbSpecification tbSpecification) {
        List<TbSpecificationOption> options = JSONObject.parseArray(jsonlist,TbSpecificationOption.class);
        boolean b = tbSpecificationService.updateBy(options, tbSpecification);
        if (b){
            return Result.response(ResultEnum.SUCCESS);
        }
        return Result.response(ResultEnum.ERROR);
    }

}
